//
//  PeopleDetailViewController.m
//  TQY
//
//  Created by 甘世清 on 2024/10/11.
//

#import "PeopleDetailViewController.h"
#import "PeopleModel.h"
#import "PeopleDetailTopView.h"
#import "MateListTableViewCell.h"
#import "MateReportPopView.h"
@interface PeopleDetailViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic , strong)PeopleDetailTopView * topView;
@property (nonatomic , strong)PeopleModel * model;
@end

@implementation PeopleDetailViewController

- (void)SQ_viewWillAppear:(BOOL)animated{
    if (self.jsonArray.count<=0) {
        [self SQ_Header];
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    WS(weakSelf);
    [self zx_setRightBtnWithImg:SQGetImage(@"gengduo") clickedBlock:^(ZXNavItemBtn * _Nonnull btn) {
        [weakSelf SQ_reportButtonClick];
    }];
}

- (void)SQ_UIViewForSelfView{
    UIImageView * bgImageView = [[UIImageView alloc]init];
    bgImageView.image = SQGetImage(@"二级背景");
    [self.view addSubview:bgImageView];
    [bgImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.equalTo(self.view);
        make.height.mas_equalTo(TopHeight+100);
    }];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.tableHeaderView = self.topView;
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    [self SQ_tableViewAddRefresh];
    WS(weakSelf);
    [self.topView.rightBtn setTouchBlock:^(SQCustomButton * _Nonnull button) {
        [weakSelf SQ_IMMessageButtonClick];
    }];
}


- (void)SQ_Header{
    [self SQ_HeaderView];
    NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
    [dic setObject:@"1" forKey:@"current"];
    [dic setObject:@"20" forKey:@"size"];
    [dic setObject:@"0" forKey:@"type"];
    [dic setObject:self.uid forKey:@"userId"];
    if ([UserModel share].lat > 0) {
        [dic setObject:@([UserModel share].lat) forKey:@"lat"];
        [dic setObject:@([UserModel share].lng) forKey:@"lng"];
    }
    [HttpTool post:@"lug/list" paramDic:dic successBlock:^(BaseModel * _Nonnull httpModel) {
        self.page = 1;
        [self.tableView.mj_header endRefreshing];
        NSDictionary * data = [[NSDictionary alloc]initWithDictionary:httpModel.data];
        NSArray * array = [MateModel mj_objectArrayWithKeyValuesArray:[data objectForKey:@"records"]];
        [self.jsonArray removeAllObjects];
        [self.jsonArray addObjectsFromArray:array];
        [self.tableView reloadData];
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
        [self.tableView.mj_header endRefreshing];
        [YJProgressHUD showMsgWithoutView:httpModel.message];
    }];
}
- (void)SQ_Foother{
    NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
    [dic setObject:[NSString stringWithFormat:@"%d",self.page+1] forKey:@"current"];
    [dic setObject:@"20" forKey:@"size"];
    [dic setObject:@"0" forKey:@"type"];
    if ([UserModel share].lat > 0) {
        [dic setObject:@([UserModel share].lat) forKey:@"lat"];
        [dic setObject:@([UserModel share].lng) forKey:@"lng"];
    }
    [dic setObject:self.uid forKey:@"userId"];
    [HttpTool post:@"lug/list" paramDic:dic successBlock:^(BaseModel * _Nonnull httpModel) {
        self.page ++;
        NSDictionary * data = [[NSDictionary alloc]initWithDictionary:httpModel.data];
        NSArray * array = [MateModel mj_objectArrayWithKeyValuesArray:[data objectForKey:@"records"]];
        [self.jsonArray addObjectsFromArray:array];
        [self.tableView reloadData];
        [self.tableView.mj_footer endRefreshing];
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
        [self.tableView.mj_footer endRefreshing];
    }];
}

- (void)SQ_HeaderView{
    NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
    [dic setObject:self.uid forKey:@"uid"];
    if ([UserModel share].lat > 0) {
        [dic setObject:@([UserModel share].lat) forKey:@"lat"];
        [dic setObject:@([UserModel share].lng) forKey:@"lng"];
    }
    [HttpTool post:@"user/userInfo" paramDic:dic successBlock:^(BaseModel * _Nonnull httpModel) {
        self.model = [PeopleModel mj_objectWithKeyValues:httpModel.data];
        self.zx_navTitle = self.model.name;
        self.topView.model = self.model;
        [self.tableView reloadData];
     
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
        
    }];
}
- (void)SQ_addFriend{
    [YJProgressHUD SQ_showMsgWithoutView:@""];
    NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
    [dic setObject:self.uid forKey:@"friendId"];
    [HttpTool post:@"im/friend/add" paramDic:dic successBlock:^(BaseModel * _Nonnull httpModel) {
        self.model.isFriend = YES;
        [self SQ_pushMessageView];
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
        
    }];
}
-(void)SQ_IMMessageButtonClick{
    NSArray * controllers = self.navigationController.viewControllers;
    BOOL isFromPeople = NO;
    for (BaseViewController * vc in controllers) {
        if ( [vc isKindOfClass:[IMMessageViewController class]]) {
            isFromPeople = YES; //是否是从消息页面跳转过来的
        }
    }
    if (isFromPeople) {
        [self SQ_popViewVC]; //如果是消息页面跳转来的就返回
    } else {
        if ([UserModel share].isVip) { //如果是会员
            if (self.model.isFriend) { //是否是好友
                [self SQ_pushMessageView]; //跳转到聊天页面
            }else{
                [self SQ_addFriend]; //不是好友就添加好友
            }
        } else {
            [VipPopView SQ_Show]; //不是会员就拉起会员
        }
    }
}
-(void)SQ_pushMessageView{
    NSString  * roomID = [NSString stringWithFormat:@"%@_%@",[UserModel share].userid,self.uid];//房间id
    IMListModel * listModel = [TQYFMDBManager SQ_roomIDGetIMListModel:roomID];
    if (![listModel.toUserPortrait isNotEmpty]) {
        listModel.type = @"0";
        listModel.content = @"";
        listModel.sendTime = [NSString SQ_currentTime];
    }
    listModel.toUser = self.uid;
    listModel.toUserName = self.model.name;
    listModel.toUserPortrait = self.model.portrait;
    listModel.roomId = roomID;
    listModel.messageCount = 0;
    [TQYFMDBManager SQ_addListModel:listModel];
    IMMessageViewController * vc = [[IMMessageViewController alloc]init];
    vc.listModel = listModel;
    [self.navigationController pushViewController:vc animated:YES];
}

-(void)SQ_reportButtonClick{
    MateReportPopView * vc = [[MateReportPopView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDETH, 260+FootHeight)];
    MateModel * model = [MateModel new];
    model.uid = self.uid;
    vc.model = model;
    vc.type  = @"1";
    LSTPopView * pop = [LSTPopView initWithCustomView:vc
                                             parentView:nil
                                               popStyle:LSTPopStyleSmoothFromBottom dismissStyle:LSTDismissStyleSmoothToBottom];
    pop.hemStyle = LSTHemStyleBottom;
  
    [pop pop];
    LSTPopViewWK(pop)
    [pop setBgClickBlock:^{
        [wk_pop dismiss];
    }];
    [vc.moreBtn setTouchBlock:^(SQCustomButton * _Nonnull button) {
        [wk_pop dismiss];
    }];
    WS(weakSelf);
    [vc setHideMatetouchBlock:^{
        [wk_pop dismiss];
        [weakSelf SQ_popViewVC];
    }];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return  self.jsonArray.count;
}
 
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MateListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MateListTableViewCell"];
    if (!cell) {
        cell = [[MateListTableViewCell alloc] initWithStyle:(UITableViewCellStyleValue1) reuseIdentifier:@"MateListTableViewCell"];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = [self.jsonArray  objectAtIndex:indexPath.item];
    WS(weakSelf);
    [cell setTouchBlock:^(MateModel * _Nonnull model) {
        [weakSelf.jsonArray removeObject:model];
        [weakSelf.tableView reloadData];
    }];
    return cell;
}
 

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return [MateListTableViewCell FSCellHeightForTableView:tableView indexPath:indexPath cellContentViewWidth:0 bottomOffset:10];
}
 
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 50;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDETH, 50)];
    view.backgroundColor = UIColor.whiteColor;
    UILabel * la = [[UILabel alloc]init];
    la.text = SQNSLocalString(@"发布的搭子");
    la.textColor = UIColor.blackColor;
    la.font = FONT_BOLD(16);
    [view addSubview:la];
    [la mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(view).offset(20);
            make.centerY.equalTo(view);
    }];
    return view;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat offSetH = scrollView.contentOffset.y;
    if (offSetH > 200) {
        self.zx_navBarBackgroundColor = UIColor.whiteColor;
    } else {
        self.zx_navBarBackgroundColor = UIColor.clearColor;
    }
}
- (PeopleDetailTopView *)topView{
    if (!_topView) {
        _topView = [[PeopleDetailTopView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDETH, SCREEN_HEIGHT-100)];
    }
    return _topView;
}

@end
